#include "string"
#include "startup.h"
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <winsock2.h>

extern "C"
{
    #include "systemdata.h"
    #include "Parse.h"
    #include "memdb.h"
    #include "templatelist.h"
};

using namespace std;

/************************************************************************/
/* Summary:	Start the server, begins to listening connection from clients
/* Return:	N/A
/* Param0:	The configuration object
/* Author:	NextLine
/* Date:		2010-5-3
/* Revise:	//
/************************************************************************/
void start_server(ConfigManager *config);

/************************************************************************/
/* Summary:	Convert the dataset struct into a stream, preparing for later sending
/* Return:	N/A
/* Param0:	The dataset which will be converted
/* Param1:	The buffer to be converted to
/* Param2:	The initial max_len of this buffer
/* Author:	NextLine
/* Date:		2010-5-3
/* Revise:	//
/************************************************************************/
int convert_dataset( DataSet* p_dataset, void** buffer , int max_len );

/************************************************************************/
/* Summary:	Find a column with the name specified.
/* Return:	N/A
/* Param0:	The command string
/* Param1:	The current database, ie. which database the script will effect
/* Param2:	The socket with which to communicate with the client
/* Author:	NextLine
/* Date:		2010-6-2
/* Revise:	//
/************************************************************************/
void execute_client_script( char * szCommand, database ** pp_cur_db, SOCKET sock );

/************************************************************************/
/* Summary:	A new thread to serve one client
/* Return:	N/A
/* Param0:	A struct containing a socket and a configManger
/* Author:	NextLine
/* Date:		2010-6-2
/* Revise:	//
/************************************************************************/
DWORD WINAPI service_thread( LPVOID lpParam );

/************************************************************************/
/* Summary:	Send the data back to the client
/* Return:	Whether this sending has finished successfully
/* Param0:	The socket linking the client
/* Param1:	The result which will be send
/* Author:	NextLine
/* Date:		2010-6-2
/* Revise:	//
/************************************************************************/
BOOL send_data_back(SOCKET sock , result_set_t* p_result);

/************************************************************************/
/* Summary:	Login to the server
/* Return:	If it has loged in successfully
/* Param0:	The login command
/* Param1:	The socket linking the client
/* Author:	NextLine
/* Date:		2010-6-2
/* Revise:	//
/************************************************************************/
BOOL_EX remote_login( char * szCommand, SOCKET sock);